using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

public class WebsiteService : Service
{
    private int websiteId;
    private string websiteGuid;

    public int WebsiteId
    {
        get { return websiteId; }
        set { websiteId = value; }
    }

    public string WebsiteGuid
    {
        get { return websiteGuid; }
        set { websiteGuid = value; }
    }

    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public WebsiteService() { }

    protected WebsiteService(
        int websiteId,
        string websiteGuid,
        int serviceId,
        int accountId,
        string title,
        DateTime createDate,
        DateTime updateDate)
        : base(serviceId, accountId, title, createDate, updateDate)
    {
        this.websiteId = websiteId;
        this.websiteGuid = websiteGuid;
    }

    internal static WebsiteService Create(string title, int accountId)
    {
        int serviceId, websiteId;
        string guid = Guid.NewGuid().ToString();

        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("WebsiteCreate", connection);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.AddWithValue("title", title);
            command.Parameters.AddWithValue("guid", guid);
            command.Parameters.AddWithValue("accountId", accountId);

            command.Parameters.Add("websiteId", SqlDbType.Int).Direction = ParameterDirection.Output;
            command.Parameters.Add("serviceId", SqlDbType.Int).Direction = ParameterDirection.Output;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            websiteId = (int)command.Parameters["websiteId"].Value;
            serviceId = (int)command.Parameters["serviceId"].Value;
        }

        return new WebsiteService(
            websiteId, guid, serviceId, accountId, title, DateTime.Now, DateTime.Now);
    }

    internal static List<WebsiteService> Fetch()
    {
        List<WebsiteService> websiteList = new List<WebsiteService>();
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("WebsiteFetch", connection);
            command.CommandType = CommandType.StoredProcedure;

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                WebsiteService website = new WebsiteService(
                    (int)reader["WebsiteId"],
                    (string)reader["Guid"],
                    (int)reader["ServiceId"],
                    (int)reader["AccountId"],
                    (string)reader["Title"],
                    (DateTime)reader["CreateDate"],
                    (DateTime)reader["UpdateDate"]);

                websiteList.Add(website);
            }
            connection.Close();
        }
        return websiteList;
    }
}